package com.fy.service.imbusiness.controller;

import javax.validation.Valid;

import com.fy.service.service.SkImUserRequestService;
import org.springframework.web.bind.annotation.*;

import com.fy.framework.common.model.R;
import com.fy.framework.component.config.LocaleMessage;
import com.fy.common.model.PageRequest;
import com.fy.common.util.TokenUtil;
import com.fy.common.dto.UserReqestDto;
import com.fy.common.dto.UserRequestHandleDto;
import com.fy.common.vo.UserRequestVo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * @author zhshuo
 * @date 2021/12/02 17:06
 **/
@Slf4j
@RestController
@AllArgsConstructor
@Api(tags = "用户好友以及群组的申请记录")
@RequestMapping("/user/request")
public class SkImUserRequestController {

    LocaleMessage localeMessage;

    SkImUserRequestService userRequestService;

    /**
     * 添加好友或者进入群组的申请
     * 
     * @return {@link R}
     */
    @PostMapping
    @ApiOperation("添加好友或者进入群组的申请")
    public R<?> pull(@RequestBody UserReqestDto userReqestDto) {
        return userRequestService.pull(userReqestDto.setSkImUserId(TokenUtil.getUserId()));
    }

    /**
     * 查询指定用户的请求列表
     * 
     * @return {@link R}
     */
    @PostMapping("/list")
    @ApiOperation("分页查询指定用户的申请记录")
    @ApiImplicitParams({@ApiImplicitParam(name = "current", value = "当前页", required = true)})
    public R<?> list(@RequestBody PageRequest<UserRequestVo> requestPageRequest) {
        return userRequestService.list(requestPageRequest);
    }

    /**
     * 请求处理
     * 
     * @return {@link R}
     */
    @ApiOperation("请求处理")
    @PostMapping("/handle")
    public R<?> handle(@RequestBody @Valid UserRequestHandleDto requestHandleDto) {
        try {
            return userRequestService.handle(requestHandleDto);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return R.failedAlter(localeMessage.getMessage("im.common.failed"));
        }
    }

}
